package top.scsoul.boot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import top.scsoul.boot.entity.Customer;
import top.scsoul.boot.dao.CustomerDao;
import top.scsoul.boot.entity.SysUser;
import top.scsoul.boot.service.CustomerService;
import org.springframework.stereotype.Service;
import top.scsoul.boot.utils.ServletUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Customer)表服务实现类
 *
 * @author 曹波涛
 * @since 2021-06-10
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param custId 主键
     * @return 实例对象
     */
    @Override
    public Customer queryById(Integer custId) {
        return customerDao.queryById(custId);
    }

    /**
     * 查询多条数据
     *
     * @param currentPage 页码
     * @param pageSize    查询条数
     * @return 对象列表
     */
    @Override
    public List<Customer> queryAllList(int currentPage, int pageSize) {
        return customerDao.queryAllByLimit((currentPage - 1) * pageSize, pageSize);
    }

    /**
     * 新增数据
     *
     * @param customer 实例对象
     * @return 实例对象
     */
    @Override
    public Customer insert(Customer customer) {
        //SysUser user = (SysUser) ServletUtil.getSession().getAttribute("user");
        customer.setCustCreatetime(new Date());
        //customer.setCustCreateId(user.getUserId());
        customerDao.insert(customer);
        return customer;
    }

    /**
     * 修改数据
     *
     * @param customer 实例对象
     * @return 实例对象
     */
    @Override
    public Customer update(Customer customer) {
        customerDao.update(customer);
        return queryById(customer.getCustId());
    }

    /**
     * 通过主键删除数据
     *
     * @param custId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer custId) {
        return customerDao.deleteById(custId) > 0;
    }

    @Override
    public int count() {
        return customerDao.count();
    }

    @Override
    public List<Customer> queryByLike(Customer customer) {
        return customerDao.queryByLike(customer);
    }
}